# -*- coding: utf-8 -*-
"""
 @Time    : 2021/1/22 下午3:30
 @Author  : tgy
 
"""
import os


class PerIter:
    __slots__ = ['_content', '_count']

    def __init__(self, content):
        self._content = content
        self._count = 2

    def __next__(self):
        if self._count == 0:
            raise StopIteration()
        print(11)
        self._count -= 1
        return 'tgy'


class Person:
    __slots__ = ['_name']

    def __init__(self, name):
        self._name = name

    def __iter__(self):
        return PerIter(self._name)


def test01():
    ...
    f = open('./re_demo.py', mode='rt', encoding='utf-8')
    print(type(f))
    # 如果是迭代对象，__iter__()返回的是自己本身
    print(type(f.__iter__()))
    # print(f.__next__())
    """
    for 循环的原理:
    1.调用对象的 __iter__()方法获取迭代对象
    2.获取到的迭代对象，调用迭代对象的 __next()__函数获取每一个值，直到出现 StopIteratorError时结束循环
    """
    # for line in f:
    #     print(line)
    print('test_replace_igon' in f)


def test02():
    ...
    # print('tgy' in 'my name is tgy')
    print('tgy' in Person('my name is tgy'))


def test03():
    l = [1, 2, 3]
    # 可迭代对象每一次调用 __iter__方法，返回的都是一个新的迭代对象
    print(l.__iter__())
    print(l.__iter__().__iter__())

    # enumerate(l)


if __name__ == '__main__':
    ...
    # test03()
    # print(os.popen('ls /'))
    for line in os.popen('ls /'):
        print(line, end='')
